<?php
declare (strict_types=1);

namespace app\admin\controller\tour;

use app\admin\controller\CommonController;
use app\common\model\BaseArea;
use app\common\model\TourTemplate;
use app\common\model\TourTemplateDetail;
use think\db\BaseQuery;

class Template extends CommonController
{
    protected $modelName = TourTemplate::MODEL_NAME;
    protected $alias = 't';

    protected function getSearchFields(): string
    {
        return "{$this->alias}.*,province.name destination_name,province.id province_id," . TourTemplate::getRouteFields();
    }

    protected function getBaseModel(): BaseQuery
    {
        return parent::getBaseModel()
            ->leftJoin(BaseArea::MODEL_NAME . ' destination', "{$this->alias}.destination_id=destination.id")
            ->leftJoin(BaseArea::MODEL_NAME . ' province', "destination.parent_id=province.id");
    }

    protected function getSearchDefaultSort(): array
    {
        return ["{$this->alias}.update_time" => 'desc',];
    }

    protected function getFilterFields(): array
    {
        return ['id', 'destination_id', 'code', 'name', 'days', 'tags', 'pic', 'pics', 'feature',
            'fee_include', 'fee_exclude', 'note', 'status', 'update_time'];
    }

    protected function validateRule(array $data): array
    {
        return [
            'code|' . lang('template.code') => 'require',
            'name|' . lang('template.name') => 'require',
            'tags|' . lang('template.tags') => 'require',
            'status|' . lang('common.status') => 'require',
        ];
    }

    protected function setDefaultValueInAddPage(array $row): array
    {
        $row['province_id'] = 0;
        $row['destination_id'] = 0;
        $row['days'] = 0;
        $row['status'] = TourTemplate::S_ENABLE;
        $row['details'] = [];
        return parent::setDefaultValueInAddPage($row);
    }

    protected function editPage(array $row)
    {
        $row['details'] = TourTemplateDetail::getListByTemplateId($row['id']);
        return parent::editPage($row);
    }

    protected function onAfterSave(array &$data, array $row = [])
    {
        $param = request()->param();
        $days = intval($param['days']);
        for ($i = 1; $i <= $days; $i++) {
            $detail = [
                'title' => $param['detail_title_' . $i]
                , 'content' => $param['detail_content_' . $i]
                , 'stay' => $param['detail_stay_' . $i]
                , 'dinner' => $param['detail_dinner_' . $i]
            ];
            if (empty($param['detail_id_' . $i])) {
                $detail['template_id'] = $data['id'];
                $detail['sort'] = $i;
                TourTemplateDetail::insert($detail);
            } else {
                TourTemplateDetail::update($param['detail_id_' . $i], $detail);
            }
        }

        // 删除明细项
        TourTemplateDetail::deleteByTemplateId($data['id'], $days, $this->recordDelete);
        parent::onAfterSave($data, $row);
    }

    protected function onBeforeDelete($id): array
    {
        // 删除明细项
        TourTemplateDetail::deleteByTemplateId($id, 0, $this->recordDelete);
        return parent::onBeforeDelete($id);
    }
}
